package com.example.dbpage.adapter

import android.arch.paging.PagedListAdapter
import android.support.v7.util.DiffUtil
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.ViewGroup
import com.example.dbpage.R
import com.example.dbpage.bean.Users

/**
 * Created by zf on 2018/7/18.
 */
class RecyclerPageAdapter : PagedListAdapter<Users, Viewholder>(diff) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Viewholder {
        return Viewholder(LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false))
    }

    override fun onBindViewHolder(holder: Viewholder, position: Int) {
        holder.textView.text = getItem(position)?.name
    }

    companion object {
        val diff = object : DiffUtil.ItemCallback<Users>() {
            override fun areContentsTheSame(oldItem: Users?, newItem: Users?): Boolean {
                return oldItem?.id == newItem?.id
            }

            override fun areItemsTheSame(oldItem: Users?, newItem: Users?): Boolean {
                return TextUtils.equals(oldItem?.name, newItem?.name)
            }
        }
    }
}